package cn.school.thoughtworks.section2;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PracticeB {
    Map<String, Integer> countSameElements(List<String> collection1) {
        //实现练习要求，并改写该行代码。
      /*  if (collection1 == null || collection1.size() == 0) {
            return null;
        }
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String temp : collection1) {
            Integer count = map.get(temp);
            if (temp.indexOf("-") > -1) {
                String str1 = temp.substring(0, temp.indexOf("-"));
                String str2 = temp.substring(str1.length() + 1);
                count = Integer.valueOf(str2);
                map.put(str1, count);
            } else {
                map.put(temp, (count == null) ? 1 : count + 1);
            }
        }
        return map;*/
        Map<String, Integer> result = new HashMap<String, Integer>();
        for (String charItemStr : collection1) {
            CharItemObj charItemObj = CharItemObj.createChByStr(charItemStr);
            String ch=charItemObj.getCh();
            Integer count =charItemObj.getCount();
            result.put(ch,result.get(ch)==null?count:result.get(ch)+count);



        }


        return result;

    }
}
